package com.mango.wall.demo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.mango.wall.MGW;

import java.util.AbstractMap;
import java.util.Locale;

public class MainActivity extends AppCompatActivity {
    private static final String USER_ID = "150150";

    private TextView available;
    private EditText integralText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView sdkVersion = findViewById(R.id.sdkVersion);
        sdkVersion.setText(String.format(Locale.getDefault(), "V%s", MGW.getVersion()));

        available = findViewById(R.id.available);
        new Thread() {
            @Override
            public void run() {
                // 同步读取可用积分
                final double integral = MGW.getIntegralSync(USER_ID);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (integral >= 0) {
                            available.setTag(integral);
                            available.setText(String.format(Locale
                                    .getDefault(), "可用积分：%.2f", integral));
                        } else
                            Toast.makeText(MainActivity.this, "积分获取失败", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        }.start();

        findViewById(R.id.generalize).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 进入应用推广页面
                MGW.gotoWall(MainActivity.this, 10006, USER_ID);
            }
        });

        integralText = findViewById(R.id.integral);
        integralText.requestFocus();
        findViewById(R.id.exchange).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                double _spend;
                try {
                    _spend = Double.parseDouble(integralText.getText().toString());
                } catch (Exception exception) {
                    return;
                }

                final double spend = _spend;
                new Thread() {
                    @Override
                    public void run() {
                        // 同步核销积分
                        final AbstractMap.SimpleEntry<Integer, String> result = MGW.exchangeSync(USER_ID, spend);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if (result != null) {
                                    if(result.getKey() == 0) {
                                        double integral = 0;
                                        if (available.getTag() != null)
                                            integral = ((double) available.getTag()) - spend;
                                        available.setText(String.format(Locale.getDefault(), "可用积分：%.2f", integral));
                                        Toast.makeText(MainActivity.this, "核销积分成功", Toast.LENGTH_SHORT).show();
                                    } else
                                        Toast.makeText(MainActivity.this, result.getValue(), Toast.LENGTH_SHORT).show();
                                } else
                                    Toast.makeText(MainActivity.this, "核销积分失败", Toast.LENGTH_SHORT).show();
                            }
                        });
                    }
                }.start();
            }
        });
    }
}